﻿using System;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace Torrenter.Common.Helpers
{
    public static class XmlSerializerHelper
    {
        //This will returns the set of included namespaces for the serializer.
        //public static XmlSerializerNamespaces GetNamespaces()
        //{

        //    XmlSerializerNamespaces ns;
        //    ns = new XmlSerializerNamespaces();
        //    ns.Add("xs", "http://www.w3.org/2001/XMLSchema");
        //    ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        //    return ns;

        //}

        ////Returns the target namespace for the serializer.
        //public static string TargetNamespace
        //{
        //    get
        //    {

        //        return "http://www.w3.org/2001/XMLSchema";
        //    }
        //}

        //Creates an object from an XML file.
        public static object FromXmlFile(string XmlFile, System.Type ObjType)
        {
            string xmlString;

            using (StreamReader reader = File.OpenText(XmlFile))
            {
                xmlString = reader.ReadToEnd();
            }
            return FromXmlString(xmlString, ObjType);
        }

        //Creates an object from an XML string.
        public static object FromXmlString(string Xml, System.Type ObjType)
        {
            XmlSerializer ser;
            ser = new XmlSerializer(ObjType);
            StringReader stringReader;
            stringReader = new StringReader(Xml);
            XmlTextReader xmlReader;
            xmlReader = new XmlTextReader(stringReader);
            object obj;
            obj = ser.Deserialize(xmlReader);
            xmlReader.Close();
            stringReader.Close();
            return obj;
        }

        //Serializes the <i>Obj</i> to an XML string.
        public static string ToXml(object Obj, System.Type ObjType)
        {
            XmlSerializer ser = new XmlSerializer(ObjType);
            //ser = new XmlSerializer(ObjType, XmlSerializerHelper.TargetNamespace);
            //ser = new XmlSerializer(ObjType);
            MemoryStream memStream = new MemoryStream();
            XmlTextWriter xmlWriter = new XmlTextWriter(memStream, Encoding.UTF8);
            //xmlWriter = new XmlTextWriter(memStream, Encoding.UTF8);
            //xmlWriter.Namespaces = true;
            //ser.Serialize(xmlWriter, Obj, XmlSerializerHelper.GetNamespaces());
            ser.Serialize(xmlWriter, Obj);
            xmlWriter.Close();
            memStream.Close();
            string xml;
            xml = Encoding.UTF8.GetString(memStream.GetBuffer());
            xml = xml.Substring(xml.IndexOf(Convert.ToChar(60)));
            xml = xml.Substring(0, (xml.LastIndexOf(Convert.ToChar(62)) + 1));
            return xml;
        }

        public static void ToFile(object obj,string filename)
        {
            ToFile(obj,obj.GetType(),filename);
        }


        public static void ToFile(object obj,Type objType,string filename)
        {
            var xml = ToXml(obj, objType);
            var f = new StreamWriter(filename);
            f.Write(xml);            
            f.Close();
        }

        public static void WriteXmlNullElement(System.Xml.XmlWriter writer, string localName)
        {
            writer.WriteStartElement(localName);
            writer.WriteAttributeString("xsi", "nil", "http://www.w3.org/2001/XMLSchema-instance", "true");
            writer.WriteEndElement();
        }
    }
}
